﻿
Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization

Public Class frmDatabaseLogin
    Public LoginData As sDatabaseLogin
    Public LoginDataPath As String = Application.StartupPath & "\ini\login.xml"
    Public SQL As cMySql

    Public Sub New()

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        LoginData = sDatabaseLogin.ReadXml(LoginDataPath)
        If IsNothing(LoginData) = False Then
            With LoginData
                If .isServerTested And .isDatabaseTested Then
                    SQL = New cMySql(.Host, .Port, .User, .Password, .Database)
                End If
            End With
        End If

    End Sub
    Private Sub frmDatabaseLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DialogResult = Windows.Forms.DialogResult.None
        LoginData = sDatabaseLogin.ReadXml(LoginDataPath)
        With LoginData
            TextBoxHost.Text = .Host
            NumericBoxPort.Value = .Port
            TextBoxBenutzer.Text = .User
            TextBoxPasswort.Text = .Password
            ComboBoxDatenbank.Text = .Database
            CheckBox1.Checked = False
        End With
        ButtonOK.Enabled = False
        ButtonDatenbankTesten.Enabled = Not String.IsNullOrWhiteSpace(ComboBoxDatenbank.Text)
    End Sub
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            TextBoxPasswort.PasswordChar = ""
        Else
            TextBoxPasswort.PasswordChar = "*"
        End If
    End Sub
    Private Sub ButtonServerTesten_Click(sender As Object, e As EventArgs) Handles ButtonServerTesten.Click
        Dim ErrorMessages As String = ""
        If cMySql.TryConnect(TextBoxHost.Text, NumericBoxPort.Value, TextBoxBenutzer.Text, TextBoxPasswort.Text, ErrorMessages) Then
            LoginData.isServerTested = True
            SQL = New cMySql(TextBoxHost.Text, NumericBoxPort.Value, TextBoxBenutzer.Text, TextBoxPasswort.Text, "")
            ComboBoxDatenbank.Items.AddRange(SQL.ReadDatabases.ToArray)
        Else
            LoginData.isServerTested = False
            ButtonOK.Enabled = False
            MsgBox("Fehlermeldung: " & ErrorMessages, MsgBoxStyle.Critical, "Verbindungsfehler")
        End If
    End Sub
    Private Sub ComboBoxDatenbank_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxDatenbank.SelectedIndexChanged
        ButtonDatenbankTesten.Enabled = Not String.IsNullOrWhiteSpace(ComboBoxDatenbank.Text)
    End Sub
    Private Sub ComboBoxDatenbank_TextChanged(sender As Object, e As EventArgs) Handles ComboBoxDatenbank.TextChanged
        ButtonDatenbankTesten.Enabled = Not String.IsNullOrWhiteSpace(ComboBoxDatenbank.Text)
    End Sub
    Private Sub ButtonDatenbankTesten_Click(sender As Object, e As EventArgs) Handles ButtonDatenbankTesten.Click
        Dim ErrorMessages As String = cMySql.TryConnect(TextBoxHost.Text, NumericBoxPort.Value, TextBoxBenutzer.Text, TextBoxPasswort.Text, ComboBoxDatenbank.Text)
        If String.IsNullOrWhiteSpace(ErrorMessages) Then
            LoginData.isDatabaseTested = True
            ButtonOK.Enabled = True
            SQL = New cMySql(TextBoxHost.Text, NumericBoxPort.Value, TextBoxBenutzer.Text, TextBoxPasswort.Text, ComboBoxDatenbank.Text)
        Else
            LoginData.isDatabaseTested = False
            ButtonOK.Enabled = False
            MsgBox("Fehlermeldung: " & ErrorMessages, MsgBoxStyle.Critical, "Verbindungsfehler")
        End If
    End Sub
    Private Sub ButtonAbbruch_Click(sender As Object, e As EventArgs) Handles ButtonAbbruch.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Hide()
    End Sub
    Private Sub ButtonOK_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        With LoginData
            .Host = TextBoxHost.Text
            .Port = NumericBoxPort.Value
            .User = TextBoxBenutzer.Text
            .Password = TextBoxPasswort.Text
            .Database = ComboBoxDatenbank.Text
            .isServerTested = True
            .isDatabaseTested = True
        End With
        sDatabaseLogin.WriteXml(LoginDataPath, LoginData)
        Me.Hide()
    End Sub

   
   
   
End Class

<Serializable> Public Class sDatabaseLogin
    Public Host As String = "127.0.0.1"
    Public Port As UInt16 = 3306
    Public User As String = "root"
    Public Password As String = "arkania"
    Public Database As String = "world"
    Public isServerTested As Boolean = False
    Public isDatabaseTested As Boolean = False
    '
    Public Shared Function ReadXml(Path As String) As sDatabaseLogin
        Try
            Dim fi As New FileInfo(Path)
            If fi.Directory.Exists = False Then fi.Directory.Create() : fi.Refresh()
            If fi.Exists Then
                Try
                    Dim xs As New XmlSerializer(GetType(sDatabaseLogin))
                    Using sr As New StreamReader(fi.FullName)
                        Return xs.Deserialize(sr)
                    End Using
                Catch ex As Exception
                    MyLogging.Message("sDatabaseLogin:ReadXml: " & ex.Message, "frmDatabaseLogin")
                End Try
            End If
        Catch ex As Exception
            MyLogging.Message("sDatabaseLogin:ReadXml: " & ex.Message, "frmDatabaseLogin")
        End Try
        Return New sDatabaseLogin
    End Function
    Public Shared Sub WriteXml(Path As String, Value As sDatabaseLogin)
        If IsNothing(Value) Then Exit Sub
        If String.IsNullOrWhiteSpace(Path) Then Exit Sub
        Try
            Dim fi As New FileInfo(Path)
            If fi.Directory.Exists = False Then fi.Directory.Create() : fi.Refresh()
            Using sw As New StreamWriter(fi.FullName, False)
                Dim xs As New XmlSerializer(GetType(sDatabaseLogin))
                xs.Serialize(sw, Value)
            End Using
        Catch ex As Exception
            MyLogging.Message("sDatabaseLogin:WriteXml: " & ex.Message, "frmDatabaseLogin")
        End Try
    End Sub
    '
End Class
